import 'package:flutter/material.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/pages/common/BaseMaterialInfo.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/pages/common/TextFieldItem.dart';
import 'package:xiongan_app_yy/pages/common/TextItemCard.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';
import 'package:xiongan_app_yy/widgets/modal_widget.dart';

class ExecutiveSamplingDetail extends StatefulWidget {
  dynamic data;
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ExecutiveSamplingDetail();
  }

  ExecutiveSamplingDetail(this.data);
}

class _ExecutiveSamplingDetail extends State<ExecutiveSamplingDetail>{
  TextEditingController controller0 = TextEditingController();
  TextEditingController controller1 = TextEditingController();
  TextEditingController controller2 = TextEditingController();
  bool downState=false;//按钮显示状态
  bool upState=false;
  bool tfState=false;//编辑框显示状态
  bool makeState=false;//盲样制作按钮状态
  bool downCad=false;
  bool upCad=false;
  Color loadBgColor = Color(0x66BCBCBC);
  bool load=false;
  @override
  void initState() {
    super.initState();
    if(widget.data["orderstate"]=="1"){
      if(widget.data["btnCheckDown"]==0&&widget.data["btnCheckUp"]==0){
        setState(() {
          downState=true;
          upState=true;
          tfState=true;
          makeState=true;
        });
      }

    }else{
      if(widget.data["btnCheckDown"]==0&&widget.data["btnCheckUp"]==0){
        setState(() {
          downState=true;
          upState=true;
          tfState=true;
        });
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
        backgroundColor: Color(0XFF00A5F1),
        title: Text(
          "执行抽样",
          style: TextStyle(fontSize: 18),
        ),
        elevation: 0,
        centerTitle: true,
      ),
      body: LoadBuilder(visibility: load,bgColor: loadBgColor,builder: Column(
        children: <Widget>[
          Expanded(
            child: Scrollbar(
              child: SingleChildScrollView(
                child: Column(
                  children: <Widget>[
                    TextItemCard(
                      name: "抽样托盘码",
                      value: widget.data["palletcode"] ?? "暂无",
                    ),
                    TextItemCard(
                      name: "抽样库区",
                      value: widget.data["areaname"] ?? "暂无",
                    ),
                    TextItemCard(
                      name: "抽样货架",
                      value: widget.data["shelfname"] ?? "暂无",
                    ),
                    TextItemCard(
                      name: "抽样货位",
                      value: widget.data["locationname"] ?? "暂无",
                    ),
                    Column(
                      children: <Widget>[
                        BaseMaterialInfo(
                          imgwid: Image.asset("assets/images/build.png",
                              fit: BoxFit.fill),
                          options: {
                            "materialname": "物资名称",
                            "materialcode": "实物ID",
                            "baseunitname": "基本单位",
                            "quantity": "计划抽样数"
                          },
                          data: widget.data,
                        ),
                        Container(
                          padding: EdgeInsets.all(15),
                          child: Column(
                            children: <Widget>[
                              Container(
                                child: Row(
                                  children: <Widget>[
                                    Expanded(
                                      child: TextFieldItem(textEditingController: controller0,
                                          hintText: "扫描或输入下架位置"),
                                    ),
                                    Offstage(offstage: downState,child: Container(
                                      margin: EdgeInsets.only(left: 10),
                                      child: FlatButton(
                                        color: Color(0xFF42AFF0),
                                        padding: EdgeInsets.only(
                                            top: 13,
                                            bottom: 13,
                                            left: 10,
                                            right: 10),
                                        child: Text(
                                          "发送指令下架",
                                          style: TextStyle(
                                              fontSize: 12,
                                              color: Colors.white),
                                        ),
                                        onPressed: () {
                                          if(controller0.text!=null&&controller0.text.length>0){
                                            setState(() {
                                              load=true;
                                            });
                                            widget.data["locationcode"]=controller0.text;
                                            sendDown();
                                          }else{
                                            TostUtil.showToast("请输入下架位置");
                                          }
                                        },
                                      ),
                                    ))

                                  ],
                                ),
                              ),
                              Container(
                                child: Row(
                                  children: <Widget>[
                                    Expanded(
                                      child: Row(
                                        children: <Widget>[
                                          Text("当前库位库存"),
                                          Expanded(
                                              child: Center(
                                                child: Text(widget
                                                    .data["actualquantity"]
                                                    .toString()),
                                              ))
                                        ],
                                      ),
                                    ),
                                    Offstage(offstage: upState,child: Container(
                                      margin: EdgeInsets.only(left: 10),
                                      child: FlatButton(
                                        color: Color(0xFF42AFF0),
                                        padding: EdgeInsets.only(
                                            top: 13,
                                            bottom: 13,
                                            left: 10,
                                            right: 10),
                                        child: Text(
                                          "下架托盘返库",
                                          style: TextStyle(
                                              fontSize: 12,
                                              color: Colors.white),
                                        ),
                                        onPressed: () {
                                          if(downCad==true){
                                            setState(() {
                                              load=true;
                                            });
                                            senUp();
                                          }else{
                                            TostUtil.showToast("请先发送下架指令");
                                          }
                                        },
                                      ),
                                    ),)

                                  ],
                                ),
                              ),
                              Offstage(
                                offstage: tfState,
                                child: Column(
                                  children: <Widget>[
                                    TextFieldItem(hintText: "关联新托盘",textEditingController: controller1,),
                                    TextFieldItem(hintText: "新托盘存放位置",textEditingController: controller2,),
                                  ],
                                ),
                              )
                            ],
                          ),
                        )
                      ],
                    )
                  ],
                ),
              ),
            ),
          ),
          Offstage(offstage: makeState,child: Row(
            children: <Widget>[
              Expanded(
                child: FlatButton(
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(0))),
                  padding: EdgeInsets.all(15),
                  color: Color(0xFF42AFF0),
                  child: Text(
                    "盲样制作",
                    style: TextStyle(color: Colors.white),
                  ),
                  onPressed: () {

                    if(tfState==true) {
                    if(controller0.text!=null&&controller0.text.length>0){
                      setState(() {
                        load=true;
                      });
                      sendMake();
                    }else{
                      TostUtil.showToast("请扫描或输入下架位置");
                    }

                    }else{
                      if(downCad==true&&upCad==true){
                        if(controller1.text!=null&&controller1.text.length>0){
                          if(controller2.text!=null&&controller2.text.length>0){
                            setState(() {
                              load=true;
                            });
                            widget.data["newpalletcode"]=controller1.text;
                            widget.data["newlocationcode"]=controller2.text;
                            sendMake();
                          }else{
                            TostUtil.showToast("请输入新托盘存放位置");
                          }
                        }else{
                          TostUtil.showToast("请关联新托盘");
                        }
                      }else{
                        TostUtil.showToast("请先完成上架和下架指令");
                      }

                    }
                  },
                ),
              )
            ],
          ),)

        ],
      ),)

    );
  }



  void sendDown() {
    InternetAPI.getInstance().post(
      "WTesting/DescSpotcheckTaskDetail/SendSpotcheckTaskTakeMaterial",
          (data){
        if(data==true){
          setState(() {
            load=false;
            downCad=true;
          });
          TostUtil.showToast("下架成功");
        }
      },
      params: widget.data,
    );
  }

  void senUp() {
    InternetAPI.getInstance().post(
      "WTesting//DescSpotcheckTaskDetail/SendSpotcheckTaskPutawayMaterial",
          (data){
        if(data==true){
          setState(() {
            load=false;
            upCad=true;
          });
          TostUtil.showToast("上架成功");
        }
      },
      params: widget.data,
    );
  }

  void sendMake() {
    InternetAPI.getInstance().post(
      "WTesting/DescSpotcheckTaskDetail/SendSpotcheckTaskMakingMaterial",
          (data){
        if(data==true){
          setState(() {
            load=false;
          });
          if (data == true) {
            Navigator.pop(context,"ok");
            TostUtil.showToast("提交成功");
          }
        }
      },
      params: widget.data,
    );
  }
}
